package com.ssy.mall.controller;

import com.ssy.mall.common.ApiRestResponse;
import com.ssy.mall.common.Constant;
import com.ssy.mall.exception.SsyMallException;
import com.ssy.mall.exception.SsyMallExceptionEnum;
import com.ssy.mall.model.pojo.User;
import com.ssy.mall.model.request.AddAdminReq;
import com.ssy.mall.model.request.AddUserReq;
import com.ssy.mall.model.request.UpdateUserReq;
import com.ssy.mall.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 用户控制器
 */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/test")
    public User personalPage() {
        return userService.getUser();
    }

    /**
     * 注册
     *
     * @return
     * @throws SsyMallException
     */
    @PostMapping("/register")
    public ApiRestResponse register(@RequestBody AddUserReq addUserReq) throws SsyMallException {
        if (StringUtils.isEmpty(addUserReq.getUsername())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(addUserReq.getPassword())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不小于8位
        if (addUserReq.getPassword().length() < 8) {
            return ApiRestResponse.error(SsyMallExceptionEnum.PASSWORD_TOO_SHORT);

        }
        userService.register(addUserReq.getUsername(),addUserReq.getPassword());
        return ApiRestResponse.success();
    }

    /**
     * 登录
     *
     * @param session
     * @return  http://192.168.3.59:8083/
     * @throws SsyMallException
     */
    @PostMapping("/login")
    public ApiRestResponse login( @RequestBody AddUserReq addUserReq,
                                  HttpSession session)
            throws SsyMallException {
        if (StringUtils.isEmpty(addUserReq.getUsername())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(addUserReq.getPassword())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(addUserReq.getUsername(),addUserReq.getPassword());
        //保存用户信息时，不保存密码
        user.setPassword(null);
        session.setAttribute(Constant.SSY_MALL_USER, user);
        return ApiRestResponse.success(user);
    }

    /**
     * 更新个性签名
     *
     * @param session
     * @return
     * @throws SsyMallException
     */
    @PostMapping("/user/update")
    public ApiRestResponse updateUserInfo(HttpSession session, @RequestBody UpdateUserReq updateUserReq) throws SsyMallException {
        User currentUser = (User) session.getAttribute(Constant.SSY_MALL_USER);
        if (currentUser == null) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        BeanUtils.copyProperties(updateUserReq,user);
        user.setUpdateTime(new Date());
        userService.updateInformation(user);
        return ApiRestResponse.success();
    }

    /**
     * 登出
     *
     * @author: ssy
     * @date: 2021/11/17
     * @description 登出清除session
     */
    @PostMapping("/user/logout")
    public ApiRestResponse logout(HttpSession session) {
        session.removeAttribute(Constant.SSY_MALL_USER);
        return ApiRestResponse.success();
    }

    /**
     * 管理员登录接口
     *
     *
     * @param session
     * @return
     * @throws SsyMallException
     */
    @PostMapping("/adminLogin")
    public ApiRestResponse adminLogin(@RequestBody AddAdminReq addAdminReq,
                                      HttpSession session)
            throws SsyMallException {
        if (StringUtils.isEmpty(addAdminReq.getUsername())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_USER_NAME);
        }
        if (StringUtils.isEmpty(addAdminReq.getPassword())) {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_PASSWORD);
        }
        User user = userService.login(addAdminReq.getUsername(), addAdminReq.getPassword());
        //校验是否是管理员
        if (userService.checkAdminRole(user)) {
            //是管理员  执行操作
            //保存用户信息时，不保存密码
            user.setPassword(null);
            session.setAttribute(Constant.SSY_MALL_USER, user);
            return ApiRestResponse.success(user);
        } else {
            return ApiRestResponse.error(SsyMallExceptionEnum.NEED_ADMIN);
        }


    }

}
